package com.zhaoc.service.impl;

import com.zhaoc.dao.CourierRepository;
import com.zhaoc.domain.Courier;
import com.zhaoc.service.ICourierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Set;

/**
 * <p>title:com.zhaoc.service.impl</p>
 * date:2018/8/1 0001
 * time:20:17
 * author:赵成
 * user:13566
 */
@Service
@Transactional
public class CourierServiceImpl implements ICourierService {

    @Autowired
    private CourierRepository courierRepository;

    //查询快递员
    @Override
    public Page<Courier> findPageData(Specification<Courier> specification,Pageable pageable) {
        //调用dao层查询分页的数据
        return courierRepository.findAll(specification,pageable);
    }

    //保存快递员
    @Override
    @RequiresPermissions("courier_add")
    public void save(Courier courier) {
        courierRepository.save(courier);
    }

    //作废快递员
    @Override
    public void delBatch(String[] idArray) {
        //调用dao层实现update方法,把快递员的状态改为1(作废)
        for (String ids:idArray) {
            Integer id = Integer.parseInt(ids);
            courierRepository.updateDeltag(id);
        }
    }

    //查询未关联定区的快递员
    @Override
    public List<Courier> findNOassaction() {
        Specification<Courier> specification = new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p = cb.isEmpty(root.get("fixedAreas").as(Set.class));
                return p;
            }
        };
        //调用dao层查询
        return courierRepository.findAll(specification);
    }
}
